/*
 * @lc app=leetcode.cn id=290 lang=javascript
 *
 * [290] 单词规律
 */
/**
 * @param {string} pattern
 * @param {string} str
 * @return {boolean}
 */
var wordPattern = function(pattern, str) {
    let strList = str.split(' ')
    let parList = pattern.split('')
    if (strList.length != parList.length) {
      return false
    }
    let result = true
    // 获取键名键值对
    let keyMap = {}
    for (let i = 0; i < strList.length; i++) {
      if (!keyMap[pattern[i]]) {
        // 判断与另一个键名的值是否相同
        // let twice = 0
        // for (let j in keyMap) {
        //   if (keyMap[j] === strList[i]) {
        //     twice++
        //     return false
        //   }
        // }
        // if (twice > 0) {
        //   return false
        // }
        if (Object.values(keyMap).indexOf(strList[i]) > -1) {
          return false
        }
        keyMap[pattern[i]] = strList[i]
      } else if (keyMap[pattern[i]] !== strList[i]) {
        // 两端的值不相等
        result = false
        break
      }
    }
    return result
};

console.log(wordPattern('abba', 'aa bb bb aa'))
